// #include <iostream>
// #include <vector>
// #include <algorithm>
// using namespace std;

// int knapsack(int W, vector<int>& weights, vector<int>& values, int n) {
//     vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));

//     for (int i = 1; i <= n; i++) {
//         for (int w = 1; w <= W; w++) {
//             if (weights[i - 1] <= w) {
//                 dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]);
//             } else {
//                 dp[i][w] = dp[i - 1][w];
//             }
//         }
//     }
//     return dp[n][W];
// }

// int main() {
//     int n, W;
//     cout << "Enter the number of items: ";
//     cin >> n;
//     cout << "Enter the capacity of the knapsack: ";
//     cin >> W;

//     vector<int> weights(n), values(n);
//     cout << "Enter the weights of the items: ";
//     for (int i = 0; i < n; i++) {
//         cin >> weights[i];
//     }
//     cout << "Enter the values of the items: ";
//     for (int i = 0; i < n; i++) {
//         cin >> values[i];
//     }

//     cout << "Maximum value in knapsack: " << knapsack(W, weights, values, n) << endl;
//     return 0;
// }



#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int knapsack(int W, vector<int>& weights, vector<int>& values, int n) {
    vector<int> dp(W + 1, 0);

    for (int i = 0; i < n; i++) {
        for (int w = W; w >= weights[i]; w--) {
            dp[w] = max(dp[w], dp[w - weights[i]] + values[i]); // 这里一定是从后往前，当前左边值（也就是左上的值）和当前值（上面的值）所以要从后面，
        }
    }
    return dp[W];
}

int main() {
    int n, W;
    cout << "Enter the number of items: ";
    cin >> n;
    cout << "Enter the capacity of the knapsack: ";
    cin >> W;

    vector<int> weights(n), values(n);
    cout << "Enter the weights of the items: ";
    for (int i = 0; i < n; i++) {
        cin >> weights[i];
    }
    cout << "Enter the values of the items: ";
    for (int i = 0; i < n; i++) {
        cin >> values[i];
    }

    cout << "Maximum value in knapsack: " << knapsack(W, weights, values, n) << endl;
    return 0;
}
